/*
 * Copyright (c) 2010-2011 GOV.
 * [Id:MenuBean.java  11-9-9 PM4:15 poplar.mumu ]
 */
package org.mumu.pawn.beans.auth;

import org.mumu.pawn.beans.common.MainPageBean;
import org.mumu.pawn.model.MenuNode;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * 系统权限树.
 * <br/>
 *
 * @author poplar.mumu
 * @version 1.0 11-9-9 PM4:15
 * @since JDK 1.5
 */
@Named("authMenu")
@ConversationScoped
public class MenuBean implements Serializable {
    private static final long serialVersionUID = 8325613727895433549L;
    private TreeNode systemMenu = null;
    private String toggledNodeData = null;

    @Inject
    private MainPageBean mainPageBean;

    public void nodeToggled(TreeToggleEvent event) {
        toggledNodeData = ((MenuNode) ((UITree) event.getComponent()).getRowData()).getTitle();
    }

    /**
     * 树形点击处理事件.
     * @param event 树形组件点击事件.
     */
    public void selectionChanged(TreeSelectionChangeEvent event) {
        UITree tree = (UITree) event.getComponent();
        Object storedRowKey = tree.getRowKey();
        for (Object rowKey : event.getNewSelection()) {
            tree.setRowKey(rowKey);
            MenuNode node = ((MenuNode) tree.getRowData());
        }
        tree.setRowKey(storedRowKey);
    }

    /**
     * 初始化菜单
     */
    public void initNodes() {
        systemMenu = new TreeNodeImpl();
        MenuNode node = new MenuNode("系统管理",true);
        node.addChild("用户管理", new MenuNode("用户管理", "/usr/list.xhtml"));
        node.addChild("部门管理", new MenuNode("部门管理", "/usr/list.xhtml"));
        node.addChild("角色管理", new MenuNode("角色管理", "/usr/list.xhtml"));
        node.addChild("权限资源", new MenuNode("权限资源", "/usr/list.xhtml"));
        node.addChild("查看日志,", new MenuNode("查看日志", "/usr/list.xhtml"));
        systemMenu.addChild("系统管理", node);

        node = new MenuNode("业务内容");
        node.addChild("2_1", new MenuNode("公文处理"));
        systemMenu.addChild("业务内容", node);

    }

    public void setSystemMenu(TreeNode systemMenu) {
        this.systemMenu = systemMenu;
    }

    public TreeNode getSystemMenu() {
        if (systemMenu == null) {
            initNodes();
        }
        return systemMenu;
    }


    public void setToggledNodeData(String toggledNodeData) {
        this.toggledNodeData = toggledNodeData;
    }

    public String getToggledNodeData() {
        return toggledNodeData;
    }
}
